/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.config.Config;
import com.manage.model.SyncConfig;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SyncConfigServiceImpl extends AbstractService implements SyncConfigService
{
	private Logger logger;
	
	public SyncConfigServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	
	@Override
	public SyncConfig getSyncConfig(int id) 
	{
		String sql = "select * from syncconfig where id=" + String.valueOf(id) + "";
		ModelGenerator<SyncConfig> dbcpmg = new ModelGenerator<SyncConfig>(SyncConfig.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<SyncConfig> getSyncConfigs()
	{
		String sql = "select * from syncconfig order by id";
		//logger.debug("sql: " + sql);
		ModelGenerator<SyncConfig> dbcpmg = new ModelGenerator<SyncConfig>(SyncConfig.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public List<SyncConfig> getSyncConfigs(int cid) {
		String sql = "select * from syncconfig where channelid=" + String.valueOf(cid) + " order by id";
		//logger.debug("sql: " + sql);
		ModelGenerator<SyncConfig> dbcpmg = new ModelGenerator<SyncConfig>(SyncConfig.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveSyncConfig(Account operator, String type, SyncConfig syncConfig)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			syncConfig.setId(getTableId("syncconfig"));
			sql = "insert into syncconfig (id,name,channelid,daylimit,totallimit,dayratio,paydaylimit,paymonthlimit) " +
					" values("+syncConfig.getId()+",'"+syncConfig.getName()+"',"+syncConfig.getChannelid()+"," +
					syncConfig.getDaylimit()+","+syncConfig.getTotallimit()+","+syncConfig.getDayratio()+
					","+syncConfig.getPaydaylimit()+","+syncConfig.getPaymonthlimit()+")";
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = "update syncconfig set name = '"+syncConfig.getName()+"'" +
					",channelid='"+syncConfig.getChannelid()+"'" +
					",daylimit='"+syncConfig.getDaylimit()+"'" +
					",totallimit='"+syncConfig.getTotallimit()+"'" +
					",dayratio='"+syncConfig.getDayratio()+"'" +
					",paydaylimit='"+syncConfig.getPaydaylimit()+
					"',paymonthlimit='"+syncConfig.getPaymonthlimit()+"' where id = "+syncConfig.getId();
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from syncconfig where id = "+syncConfig.getId();
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				SyncConfig lo = null;
				if (!type.equals("delete"))
					lo = getSyncConfig(syncConfig.getId());
				logTableOperate("project", type, operator, lo);
				return true;
			}
		}		
		return false;
	}

}
